package com.java.threadsafe.vsafe3;

/*
 * 	threadlocal对象，它作为线程的一个局部变量，可以为每个线程创建一个副本，
 * 	用来保存每个线程的属性，各是各的，互不干扰。单每个 threadlocal变量只能保存一个变量，假如有多
 *  个<变量>要保存，那么就要写多个threadlocal对象。
 */
public class ThreadSafeEntityUserThreadLocal implements Runnable {
	ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
	@Override
	public void run() {
		for(int i=0; i<5; i++){
			if(threadLocal.get() == null) {
				threadLocal.set(new Integer(1));
			}
			int num = threadLocal.get();
			++num;
			threadLocal.set(new Integer(num));
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + " ---- " + num);
		}
		
	}
	
}
